package com.lzy.cloud.search.client.controller;

import com.lzy.cloud.search.client.entity.EsIndex;
import com.lzy.cloud.search.client.entity.EsQuery;
import com.lzy.cloud.search.client.service.GetDocmentService;
import com.lzy.cloud.search.client.service.TermLevelQueriesService;
import com.lzy.cloud.search.client.service.impl.FullTextQueriesServiceImpl;
import com.lzy.framework.core.output.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by WIN7 on 2018/5/21.
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private GetDocmentService getDocmentService;

    @Autowired
    private TermLevelQueriesService termLevelQueriesService;

    @Autowired
    private FullTextQueriesServiceImpl fullTextQueriesService;

    /**
     * 查询单个文档
     *
     * @param esIndex
     * @return
     */
    @RequestMapping("/document")
    @ResponseBody
    public Result index(EsIndex esIndex){
        return new Result(getDocmentService.getDocment(esIndex));
    }

    /**
     * 查询多条文档
     *
     * @return
     */
    @RequestMapping(value = "/documents", method = RequestMethod.POST)
    @ResponseBody
    public Result multiSearch(@RequestBody List<EsIndex> esIndexList)  {
        return new Result(getDocmentService.multiGet(esIndexList));
    }

    /**
     * 单个词条匹配搜索
     *
     * @return
     */
    @RequestMapping(value = "/term", method = RequestMethod.POST)
    @ResponseBody
    public Result termQuerys(EsQuery esQuery) {
        return new Result(termLevelQueriesService.termQuerys(esQuery));

    }

    /**
     * 多个词条匹配搜索
     *
     * @return
     */
    @RequestMapping(value = "/terms", method = RequestMethod.POST)
    @ResponseBody
    public Result termsQuerys(EsQuery esQuery)  {
        return new Result(termLevelQueriesService.termsQuerys(esQuery));
    }

    /**
     * 按照范围性搜索
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/range", method = RequestMethod.POST)
    @ResponseBody
    public Result rangeQuerys(EsQuery esQuery)  {
        return new Result(termLevelQueriesService.rangeQuerys(esQuery));
    }

    /**
     * 判断是否存在匹配
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/exists", method = RequestMethod.POST)
    @ResponseBody
    public Result existsQuerys(EsQuery esQuery) {
        return new Result(termLevelQueriesService.existsQuerys(esQuery));
    }

    /**
     * 前缀匹配
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/prefix", method = RequestMethod.POST)
    @ResponseBody
    public Result prefixQuerys(EsQuery esQuery)  {
        return new Result(termLevelQueriesService.prefixQuerys(esQuery));
    }


    /**
     * 通配符查询匹配
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/wildcard", method = RequestMethod.POST)
    @ResponseBody
    public Result wildcardQuerys(EsQuery esQuery) {
        return new Result(termLevelQueriesService.wildcardQuerys(esQuery));
    }

    /**
     * 正则表达式搜索
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/regexp", method = RequestMethod.POST)
    @ResponseBody
    public Result regexpQuerys(EsQuery esQuery)  {
        return new Result(termLevelQueriesService.regexpQuerys(esQuery));
    }

    /**
     * 模糊搜索
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/fuzzy", method = RequestMethod.POST)
    @ResponseBody
    public Result fuzzyQuerys(EsQuery esQuery) {
        return new Result(termLevelQueriesService.fuzzyQuerys(esQuery));
    }


    /**
     * 匹配搜索(全文)
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/match", method = RequestMethod.POST)
    @ResponseBody
    public Result matchQuerys(EsQuery esQuery) {
        return new Result(fullTextQueriesService.matchQuerys(esQuery));
    }


    /**
     * 多个字段匹配搜索(全文)
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/matches", method = RequestMethod.POST)
    @ResponseBody
    public Result multiMatchQuerys(EsQuery esQuery) {
        return new Result(fullTextQueriesService.multiMatchQuerys(esQuery));
    }


    /**
     * 常用词查询匹配搜索(全文)
     *
     * @param esQuery
     * @return
     */
    @RequestMapping(value = "/common", method = RequestMethod.POST)
    @ResponseBody
    public Result commonTermsQuerys(EsQuery esQuery){
        return new Result(fullTextQueriesService.commonTermsQuerys(esQuery));
    }
}
